package com.ohuang.xposed_tiktok.smallwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

public abstract class WindowUI implements WindowInterface{
    private ViewGroup rootView;

    private Activity activity;
    private WindowManager.LayoutParams layoutParams;
    private boolean isShow = false;
    private boolean isInit = false;

    public WindowUI(Activity activity) {
        this.activity = activity;
    }

    public void show() {
        if (!isInit) {
            init();
            isInit = true;
        }
        if (!isShow) {
            isShow = true;
            getWindowManager(activity).addView(rootView, layoutParams);


        }
    }

    public boolean isShow() {
        return isShow;
    }

    public void hide() {
        if (!isInit) {
            return;
        }
        if (isShow) {
            isShow = false;
            getWindowManager(activity).removeView(rootView);
//            mainDialog.dismiss();
        }
    }

    private void init() {
        rootView = new FrameLayout(activity);
        layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
        layoutParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;
        View view = createView(activity);
        rootView.addView(view);
        initView(view);
    }

    protected abstract View createView(Activity activity);

    protected abstract void initView(View view);
    public WindowManager getWindowManager(Activity context) {
        WindowManager systemService = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return systemService;
    }
}
